import os
import cv2
indir='data1'
outdir = 'data_jpg0.3' 
dict2img={}
for path,pathname,filenames in os.walk(outdir):
  for filename in filenames:
    if filename.endswith(".jpg"):
      dict2img[filename.replace('.jpg','')] = 1

for path,pathname,filenames in os.walk(indir):
  for filename in filenames:
    if filename.endswith((".png",".webp")):
      dir2 = path.split('/')[-1]
      odir = os.path.join(outdir,dir2)
      os.system('mkdir -p %s'%odir)
      ifname = os.path.join(path,filename)
      xname = filename.replace('.png','').replace('.webp','')
      if xname in dict2img:
        continue
      ofname = os.path.join(odir,'%s.jpg'%filename)
      image = cv2.imread(ifname)
      size = image.shape
      width = int(size[1]*0.35)
      height = int(size[0]*0.35)
      #if not width == height:
      #    continue
      resized_image = cv2.resize(image, (width, height))
      print(ofname)
      cv2.imwrite(ofname, resized_image,[cv2.IMWRITE_JPEG_QUALITY, 95])

